<?php
/**
 * Services_Omniture_Request_HTTPRequest 
 * 
 * PHP Version 5.1.0+
 * 
 * @uses      Services_Omniture_Request_Common
 * @category  Services
 * @package   Services_Omniture
 * @author    Bill Shupp <hostmaster@shupp.org> 
 * @copyright 2008 Bill Shupp
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link      http://servicesomniture.googlecode.com
 */

require_once 'Services/Omniture/Request/Common.php';
require_once 'HTTP/Request.php';

/**
 * HTTP_Request driver for Services_Omniture
 * 
 * @uses      Services_Omniture_Request_Common
 * @uses      HTTP_Request
 * @category  Services
 * @package   Services_Omniture
 * @author    Bill Shupp <hostmaster@shupp.org> 
 * @copyright 2008 Bill Shupp
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link      http://servicesomniture.googlecode.com
 */
class Services_Omniture_Request_HTTPRequest
extends Services_Omniture_Request_Common
{
    /**
     * Instance of HTTP_Request
     * 
     * @var object
     */
    protected $httpRequest;

    /**
     * Sets the options, endpoint, and instatiates HTTP_Request
     * 
     * @param string                   $uri URI of the API endpoint
     * @param Services_Omniture_Common $so  Instance of Services_Omniture_Common
     * 
     * @return void
     */
    public function __construct($uri, Services_Omniture_Common $so)
    {
        parent::__construct($uri, $so);
        $options = array('method'  => 'POST',
                         'timeout' => $this->so->getOption('timeout'));

        $this->httpRequest = new HTTP_Request($uri, $options);
    }

    /**
     * Sets a the POST body.
     * 
     * @param SimpleXMLElement $body Body contents (xml)
     * 
     * @return void
     */
    public function setBody(SimpleXMLElement $body)
    {
        $this->httpRequest->setBody($body->asXML());
    }

    /**
     * Sets the User-Agent and actually sends the request to the Omniture API.
     * 
     * @throws Services_Omniture_Exception on failure
     * @return void
     */
    public function sendRequest()
    {
        $this->httpRequest->addHeader('User-Agent',
                                      $this->so->getOption('userAgent'));
        $res = $this->httpRequest->sendRequest();
        if (PEAR::isError($res)) {
            throw new Services_Omniture_Exception(
                'Error from HTTP_Request: ' . $res->getMessage(), $res
            );
        }
    }

    /**
     * Gets the response code of the request.
     * 
     * @return int
     */
    public function getResponseCode()
    {
        return $this->httpRequest->getResponseCode();
    }

    /**
     * Gets the body of the response.
     * 
     * @return string
     */
    public function getResponseBody()
    {
        return $this->httpRequest->getResponseBody();
    }
}
?>
